<template>
  <div>
    <div class="search-header" action="/">
      <van-icon
        name="arrow-left"
        color="white"
        size="0.48rem"
        class="goback"
        @click="onCancel"
      />
      <van-search
        v-model="value"
        shape="round"
        background="#007bff"
        placeholder="请输入搜索关键词"
        @input="onSearch"
        @search="enterFn(value)"
        v-JuJiao
      />
    </div>
    <div class="sugg-list" v-if="value !== ''" @click="enterFn">
      <div
        class="sugg-item"
        v-for="(item, index) in list"
        :key="index"
        v-html="textLight(item, value)"
        @click="Search(item)"
      ></div>
    </div>
    <div class="search-history" v-else>
      <!-- 标题 -->
      <van-cell title="搜索历史">
        <!-- 使用 right-icon 插槽来自定义右侧图标 -->
        <template #right-icon>
          <van-icon name="delete" class="search-icon" @click="delHistory" />
        </template>
      </van-cell>
      <!-- 历史列表 -->
      <div class="history-list">
        <span
          class="history-item"
          v-for="(str, index) in history"
          :key="index"
          @click="search(str)"
          >{{ str }}
        </span>
      </div>
    </div>
  </div>
</template>

<script>
import { suggestListAPI } from '@/api'
export default {
  name: 'Search',
  data () {
    return {
      value: '',
      time: null,
      list: [],
      history: JSON.parse(window.localStorage.getItem('history')) || [
        'API',
        'java',
        'css',
        '前端',
        '后台接口',
        'python'
      ]
    }
  },
  methods: {
    onSearch () {
      clearTimeout(this.time)
      this.time = setTimeout(async () => {
        if (this.value === '') return
        const res = await suggestListAPI({ q: this.value })
        this.list = res.data.data.options
      }, 500)
    },
    textLight (str, value) {
      const reg = new RegExp(value, 'ig')
      return str.replace(reg, match => {
        return `<span style="color: red">${match}</span>`
      })
    },
    onCancel () {
      this.$router.push('/layout')
    },
    delHistory () {
      if (confirm('确认删除吗')) {
        console.log('删除历史搜索')
        this.history = []
        window.localStorage.removeItem('history')
      }
    },
    Search (item) {
      if (!this.history.includes(item)) {
        this.history.unshift(item)
      }
      this.value = item
    },
    enterFn (value) {
      if (!this.history.includes(value)) {
        this.history.unshift(value)
      }
      this.$router.push({
        path: `/search/${this.value}`
      })
    },
    search (str) {
      if (!this.history.includes(str)) {
        this.history.unshift(str)
      }
      this.value = str
      this.enterFn()
    }
  },
  watch: {
    history (newval) {
      window.localStorage.setItem('history', JSON.stringify(newval))
    }
  }
}
</script>

<style lang="less" soped>
.search-header {
  height: 46px;
  display: flex;
  align-items: center;
  background-color: #007bff;
  overflow: hidden;
  /*后退按钮*/
  .goback {
    padding-left: 14px;
  }
  /*搜索组件*/
  .van-search {
    flex: 1;
  }
}
.sugg-list {
  .sugg-item {
    padding: 0 15px;
    border-bottom: 1px solid #f8f8f8;
    font-size: 14px;
    line-height: 50px;
    // 实现省略号的三行代码
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
}
.search-icon {
  font-size: 16px;
  line-height: inherit;
}

.history-list {
  padding: 0 10px;
  .history-item {
    display: inline-block;
    font-size: 12px;
    padding: 8px 14px;
    background-color: #efefef;
    margin: 10px 8px 0px 8px;
    border-radius: 10px;
  }
}
</style>
